package com.blueseaapps.c2dm;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * Utilities for device registration.
 *
 * Will keep track of the registration token in a private preference.
 */
public class C2DMessaging {
	public static final String EXTRA_SENDER = "sender";
	public static final String EXTRA_APPLICATION_PENDING_INTENT = "app";
	public static final String REQUEST_UNREGISTRATION_INTENT = "com.google.android.c2dm.intent.UNREGISTER";
	public static final String REQUEST_REGISTRATION_INTENT = "com.google.android.c2dm.intent.REGISTER";
	
	public static final String PACKAGE = "com.blueseaapps";

	private static final long DEFAULT_BACKOFF = 30000;

	/**
	 * Initiate c2d messaging registration for the current application
	 */
	public static void register(Context context,
			String senderId) {
		Intent registrationIntent = new Intent(REQUEST_REGISTRATION_INTENT);
		registrationIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
				PendingIntent.getBroadcast(context, 0, new Intent(), 0));
		registrationIntent.putExtra(EXTRA_SENDER, senderId);
		context.startService(registrationIntent);
	}

	/**
	 * Unregister the application. New messages will be blocked by server.
	 */
	public static void unregister(Context context) {
		Intent regIntent = new Intent(REQUEST_UNREGISTRATION_INTENT);
		regIntent.setPackage(PACKAGE);
		regIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT, PendingIntent.getBroadcast(context,
				0, new Intent(), 0));
		context.startService(regIntent);
	}
}
